﻿using System;
using System.Collections.Generic;
using System.IO; 
using System.Text;
using System.Web;
using System.Configuration;

/// <summary>
///CreatFile 的摘要说明
/// </summary>
public class CreatFile
{
	public CreatFile()
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
	}

    public static void  CreateFileHelper(string  filename,string  writeStream)
    {

        string path = ConfigurationManager.AppSettings["FileCreat"];
        if (filename.ToLower().IndexOf("dao") > 0)
        {
            path = path + @"\Dao";
        }
        if (filename.StartsWith("I"))
        {
            path = path + @"\Iservice";
        }
        if (filename.EndsWith("service.cs") && (!filename.StartsWith("I")))
        {
            path = path + @"\Service";
        }
        if (filename.ToLower().IndexOf("model") > 0)
        {
            path = path + @"\Model";
        }
        if (filename.ToLower().IndexOf("detail.aspx") > 0)
        {
            path = path + @"\detail";
        }
        if (filename.ToLower().IndexOf("view") > 0)
        {
            path = path + @"\view";
        }
        if (filename.ToLower().IndexOf("xml") > 0)
        {
            path = path + @"\xml";
        }
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }
        if (!File.Exists(path + @"\" + filename))
        {
            File.Create(path + @"\" + filename).Close();
        }
        //else {
        //    File.Delete(path + @"\" + filename);
        //}
      
        StreamReader reader = null;
        string str2 = string.Empty;
        StreamWriter writer = null;

        try
        {
            reader = new StreamReader(path + @"\" + filename);
            reader.ReadToEnd();
            str2 = writeStream;
            reader.Close();
            Encoding encoding = Encoding.GetEncoding("utf-8");
            writer = new StreamWriter(path + @"\" + filename, false, encoding);
            writer.Write(str2);
            writer.Flush();

        }
        catch (Exception)
        {

            throw;
        }finally
        {
            if (reader != null)
            {
                reader.Close();
            }
            if (writer != null)
            {
                writer.Close();
            }

            
        }
    }

}